<template>
  <el-card header="登陆密码修改">
    <el-form ref="pwdSet" :model="emp" :rules="rules" label-width="120px">
      <el-form-item label="密码" prop="Pwd">
        <el-input
          v-model="emp.Pwd"
          type="password"
          maxlength="30"
          placeholder="密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="新密码" prop="NewPwd">
        <el-input
          v-model="emp.NewPwd"
          type="password"
          maxlength="30"
          placeholder="密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="重复密码" prop="RepeatPwd">
        <el-input
          v-model="emp.RepeatPwd"
          type="password"
          maxlength="30"
          placeholder="重复密码"
          show-password
        />
      </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="reset">重置</el-button>
    </div>
  </el-card>
</template>

<script>
import { setPwd } from '@/api/emp/loginUser'
export default {
  components: {
  },
  data() {
    const checkPwd = (rule, value, callback) => {
      if (value == null) {
        callback()
      } else if (value !== this.emp.NewPwd) {
        callback(new Error('两次密码需输入一致!'))
      } else {
        callback()
      }
    }
    return {
      emp: {
        RepeatPwd: null,
        NewPwd: null,
        Pwd: null
      },
      rules: {
        Pwd: [
          {
            required: true,
            message: '原密码不能为空！',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 30,
            message: '密码长度需在6-30位之间！',
            trigger: 'blur'
          }
        ],
        NewPwd: [
          {
            required: true,
            message: '密码不能为空！',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 30,
            message: '密码长度需在6-30位之间！',
            trigger: 'blur'
          }
        ],
        RepeatPwd: [
          {
            required: true,
            message: '重复密码不能为空！',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 30,
            message: '密码长度需在6-30位之间！',
            trigger: 'blur'
          },
          {
            validator: checkPwd,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    save() {
      this.$refs['pwdSet'].validate((valid) => {
        if (valid) {
          this.set()
        } else {
          return false
        }
      })
    },
    reset() {
      this.emp = {
        RepeatPwd: null,
        NewPwd: null,
        Pwd: null
      }
    },
    async set() {
      await setPwd(this.emp.Pwd, this.emp.NewPwd)
      this.$message({
        message: '密码修改成功!',
        type: 'success'
      })
    }
  }
}
</script>
